package com.sunlands.deskmate.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sunlands.deskmate.entity.TzChatRecord;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * @author 
 */
@Data
@ToString
public class TzChatRecordVO implements Serializable {
    /**
     * 主键
     */
    private String id;

    /**
     * 发送者用户ID
     */
    private String fromUserId;

    /**
     * 消息类型 依据长链接数据定义
     */
    private String type;

    /**
     * 消息内容
     */
    private String message;

    /**
     * 时间戳
     */
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date createTime;

    /**
     * 额外数据
     */
    private JSONObject extras;

    /**
     * 发送到哪
     */
    private String toId;

    /**
     * 业务id，用来承载业务相关id，如群id、房间id，一般分享、邀请的时候会用到
     */
    private String contentId;

    /**
     * contentId 的 type
     */
    private String contentType;

    private Long ackId;

    private static final long serialVersionUID = 1L;

    public static TzChatRecordVO from(TzChatRecord r){
        TzChatRecordVO vo = new TzChatRecordVO();
        vo.setId(r.getId() == null ? "" : r.getId().toString());
        vo.setContentId(r.getContentId());
        vo.setContentType(r.getContentType());
        vo.setCreateTime(r.getCreateTime());
        vo.setToId(r.getToId());
        vo.setType(r.getType());
        vo.setExtras(JSON.parseObject(r.getExtras()));
        vo.setFromUserId(r.getFromUserId());
        vo.setMessage(r.getMessage());
        vo.setId(Objects.nonNull(r.getAckId()) ? r.getAckId().toString() : vo.getId());
        vo.setAckId(r.getAckId());
        return vo;
    }
}